<?php
/**
 * Created by PhpStorm.
 * User: jingshou
 * Date: 2017/9/10
 * Time: 15:42
 */

namespace app\admin\model;

use think\Db;
use think\Image;
use think\Model;
use think\Session;

class Leva extends Model
{
    const TYPE = 'leva';
    const STATE_0 = 0;
    const STATE_1 = 1;
    protected static $_stateList;

    public function getTypeNameAttr()
    {
        $typeInfo = Type::get($this->typeId);
        return !empty($typeInfo) ? $typeInfo->typeName : '';
    }

    public function getStateMsgAttr()
    {
        $list = self::getStateList();
        return !empty($list) ? $list[$this->state] : null;
    }

    public static function getStateList()
    {
        if (self::$_stateList === null) {
            self::$_stateList = [
                self::STATE_0 => '未审核',
                self::STATE_1 => '已查看',
            ];
        }
        return self::$_stateList;
    }

    public static function newsList(array $condition)
    {
        $query = new self();

        if (!empty($condition['id'])) {
            $query->where(['id' => $condition['id']]);
        }

        if (!empty($condition['name'])) {
            $query->where('name', 'like', '%' . $condition['name'] . '%');
        }

        return $query->order('id desc')->paginate(20, false, [
            'query' => $condition
        ]);
    }

    public static function add(array $data)
    {
        Db::startTrans();
        try {
            $model = new Link();
            $model->data($data);
            $model->language = Session::get('language');
            $model->created = time();

            if ($model->save() == 0) {
                return resError($model->getError());
            }
            Db::commit();
            return resData('添加成功', \url('type_list'));
        } catch (\Exception $e) {
            Db::rollback();
            return resError($e->getMessage());
        }
    }

    public static function newsUpdate(array $data)
    {

        $model = self::get($data['id']);
        if (!empty($_FILES['img']['name'])) {
            @unlink($path = ROOT_PATH . 'public' . DS . 'upload' . DS . replace_img($model->img));
        }
        $model->data($data);
        if ($model->save() == 0) {
            return resError('更新失败');
        }
        return resData('更新成功', \url('news_list'), $model->toArray());
    }

    public static function newsDel($id)
    {
        $model = self::get($id);
        if ($model->delete() > 0) {
            return resData('删除成功');
        } else {
            return resError('删除失败');
        }
    }

    public static function upload()
    {
        $file = request()->file('img');
        $path = ROOT_PATH . 'public' . DS . 'upload';
        $info = $file->validate(['size' => 2048000, 'ext' => 'jpg,png,gif'])->move($path);
        if ($info) {
            $image = Image::open($path . DS . $info->getSaveName());
            $image->thumb(142, 98)->save($path . DS . $info->getSaveName());
            return resData('', '', ['img_url' => $info->getSaveName()]);
        } else {
            return resError($file->getError());
        }
    }
}